﻿'
' Quiz Module for DotNetNuke
' Project Contributors - Will Strohl (http://www.WillStrohl.com)
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Quiz Module for DotNetNuke, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports Microsoft.ApplicationBlocks.Data
Imports System
Imports System.Data
Imports System.Data.SqlClient

Namespace WillStrohl.Modules.Quiz

    Public Class SqlDataProvider
        Inherits DataProvider

#Region " Private Members "

        Private Const ProviderType As String = "data"

        Private p_providerConfiguration As Framework.Providers.ProviderConfiguration = Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)
        Private p_connectionString As String = String.Empty
        Private p_providerPath As String = String.Empty
        Private p_objectQualifier As String = String.Empty
        Private p_databaseOwner As String = String.Empty

        ' local string values
        Private Const c_ConnectionStringName As String = "connectionStringName"
        Private Const c_ConnectionString As String = "connectionString"
        Private Const c_ProviderPath As String = "providerPath"
        Private Const c_ObjectQualifier As String = "objectQualifier"
        Private Const c_Underscore As String = "_"
        Private Const c_DatabaseOwner As String = "databaseOwner"
        Private Const c_Period As String = "."
        Private Const c_SProc_Prefix As String = "wns_quiz_"

        ' sproc names
        Private Const c_AddQuiz As String = "AddQuiz"
        Private Const c_UpdateQuiz As String = "UpdateQuiz"
        Private Const c_DeleteQuiz As String = "DeleteQuiz"
        Private Const c_GetQuiz As String = "GetQuiz"
        Private Const c_GetQuizCount As String = "GetQuizCount"
        Private Const c_GetQuizzes As String = "GetQuizzes"

        Private Const c_GetQuestion As String = "GetQuestion"
        Private Const c_GetQuestionCount As String = "GetQuestionCount"
        Private Const c_GetQuestions As String = "GetQuestions"
        Private Const c_AddQuestion As String = "AddQuestion"
        Private Const c_UpdateQuestion As String = "UpdateQuestion"
        Private Const c_DeleteQuestion As String = "DeleteQuestion"

        Private Const c_GetAnswer As String = "GetAnswer"
        Private Const c_GetAnswers As String = "GetAnswers"
        Private Const c_AddAnswer As String = "AddAnswer"
        Private Const c_UpdateAnswer As String = "UpdateAnswer"
        Private Const c_DeleteAnswer As String = "DeleteAnswer"

#End Region

#Region " Constructors "

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' 20101022 - wstrohl - Changed the connection string to use a different method of retrieval
        ''' </history>
        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Framework.Providers.Provider = CType(p_providerConfiguration.Providers(p_providerConfiguration.DefaultProvider), Framework.Providers.Provider)

            ' Read the attributes for this provider
            If Not String.IsNullOrEmpty(objProvider.Attributes(c_ConnectionStringName)) Then
                p_connectionString = Utilities.Config.GetConnectionString
            Else
                p_connectionString = objProvider.Attributes(c_ConnectionString)
            End If

            p_providerPath = objProvider.Attributes(c_ProviderPath)

            p_objectQualifier = objProvider.Attributes(c_ObjectQualifier)
            If Not String.IsNullOrEmpty(p_objectQualifier) And p_objectQualifier.EndsWith(c_Underscore) = False Then
                p_objectQualifier = String.Concat(p_objectQualifier, c_Underscore)
            End If

            ' Add willstrohl_ to the beginning of the sprocs
            p_objectQualifier = String.Concat(p_objectQualifier, c_SProc_Prefix)

            p_databaseOwner = objProvider.Attributes(c_DatabaseOwner)
            If Not String.IsNullOrEmpty(p_databaseOwner) And p_databaseOwner.EndsWith(c_Period) = False Then
                p_databaseOwner = String.Concat(p_databaseOwner, c_Period)
            End If

        End Sub

#End Region

#Region " Properties "

        Public ReadOnly Property ConnectionString() As String
            Get
                Return p_connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return p_providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return p_objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return p_databaseOwner
            End Get
        End Property

#End Region

#Region " Data Provider Implementation "

        Public Overrides Function AddQuiz(ByVal PortalId As Integer, ByVal QuizName As String, ByVal QuizDescription As String, ByVal RandomizeQuestions As Boolean, ByVal QuestionCount As Integer, ByVal BeginDate As DateTime, ByVal EndDate As DateTime, ByVal LastModifiedBy As Integer) As Integer
            If EndDate = Null.NullDate Then
                EndDate = DateTime.Parse("1/1/1900")
            End If
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_AddQuiz), PortalId, GetNull(QuizName), GetNull(QuizDescription), RandomizeQuestions, QuestionCount, BeginDate, EndDate, LastModifiedBy), Integer)
        End Function
        Public Overrides Sub UpdateQuiz(ByVal QuizId As Integer, ByVal PortalId As Integer, ByVal QuizName As String, ByVal QuizDescription As String, ByVal RandomizeQuestions As Boolean, ByVal QuestionCount As Integer, ByVal BeginDate As DateTime, ByVal EndDate As DateTime, ByVal LastModifiedBy As Integer)
            If EndDate = Null.NullDate Then
                EndDate = DateTime.Parse("1/1/1900")
            End If
            SqlHelper.ExecuteNonQuery(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_UpdateQuiz), QuizId, PortalId, GetNull(QuizName), GetNull(QuizDescription), RandomizeQuestions, QuestionCount, BeginDate, EndDate, LastModifiedBy)
        End Sub
        Public Overrides Sub DeleteQuiz(ByVal QuizId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_DeleteQuiz), QuizId)
        End Sub
        Public Overrides Function GetQuiz(ByVal QuizId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetQuiz), QuizId), IDataReader)
        End Function
        Public Overrides Function GetQuizCount(ByVal PortalId As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetQuizCount), PortalId), Integer)
        End Function
        Public Overrides Function GetQuizzes(ByVal PortalId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetQuizzes), PortalId), IDataReader)
        End Function


        Public Overrides Function AddQuestion(QuizId As Integer, Question As String, QuestionType As String, RandomizeAnswers As Boolean, IsActive As Boolean, DisplayOrder As Integer, LastModifiedBy As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_AddQuestion), QuizId, GetNull(Question), GetNull(QuestionType), RandomizeAnswers, IsActive, DisplayOrder, LastModifiedBy), Integer)
        End Function
        Public Overrides Sub DeleteQuestion(QuestionId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_DeleteQuestion), QuestionId)
        End Sub
        Public Overrides Function GetQuestion(QuestionId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetQuestion), QuestionId), IDataReader)
        End Function
        Public Overrides Function GetQuestionCount(QuizId As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetQuestionCount), QuizId), Integer)
        End Function
        Public Overrides Function GetQuestions(QuizId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetQuestions), QuizId), IDataReader)
        End Function
        Public Overrides Sub UpdateQuestion(QuestionId As Integer, QuizId As Integer, Question As String, QuestionType As String, RandomizeAnswers As Boolean, IsActive As Boolean, DisplayOrder As Integer, LastModifiedBy As Integer)
            SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_UpdateQuestion), QuestionId, QuizId, GetNull(Question), GetNull(QuestionType), RandomizeAnswers, IsActive, DisplayOrder, LastModifiedBy)
        End Sub


        Public Overrides Function AddAnswer(QuestionId As Integer, Answer As String, ByVal IsCorrect As Boolean, IsActive As Boolean, DisplayOrder As Integer, LastModifiedBy As Integer) As Integer
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_AddAnswer), QuestionId, GetNull(Answer), IsCorrect, IsActive, DisplayOrder, LastModifiedBy), Integer)
        End Function
        Public Overrides Sub DeleteAnswer(AnswerId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_DeleteAnswer), AnswerId)
        End Sub
        Public Overrides Function GetAnswer(AnswerId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetAnswer), AnswerId), IDataReader)
        End Function
        Public Overrides Function GetAnswers(QuestionId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_GetAnswers), QuestionId), IDataReader)
        End Function
        Public Overrides Sub UpdateAnswer(AnswerId As Integer, QuestionId As Integer, Answer As String, ByVal IsCorrect As Boolean, IsActive As Boolean, DisplayOrder As Integer, LastModifiedBy As Integer)
            SqlHelper.ExecuteScalar(ConnectionString, String.Concat(DatabaseOwner, ObjectQualifier, c_UpdateAnswer), AnswerId, QuestionId, GetNull(Answer), IsCorrect, IsActive, DisplayOrder, LastModifiedBy)
        End Sub

#End Region

#Region " Private Helper Methods "

        Private Function GetNull(ByVal Field As Object) As Object
            Return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value)
        End Function

#End Region

    End Class

End Namespace